const chooseLocation = requirePlugin('chooseLocation')
const App = getApp();
import WxValidate from '../../../../assets/plugins/wx-validate/WxValidate'
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
import utils from '../../../../utils/utils.js';
Page({
    data: {
        fileList: [],
        apply: {
            contact_name: '',
            contact_phone: '',
            company_name: '',
            contact_addr: '',
        },
        checked:false,
        show: true,
    },
  showPopup() {
    wx.previewImage({
      urls: [App.globalData.config.configList.contract]
    });
  },

    onChange(){
      this.setData({checked:!this.data.checked})
    },
    formSubmit: function (e) {
        const _this = this
        if (!this.data.checked){
          Toast('请同意阅读合同');
          return
        }
        var imgList = _this.data.fileList.map(x => {
            return JSON.parse(x.pic)[0]
        })

        const params = Object.assign(
            _this.data.apply, e.detail.value, {
                'attachment': JSON.stringify(imgList)
            }
        )
        // 传入表单数据，调用验证方法
        if (!this.WxValidate.checkForm(params)) {
            const error = this.WxValidate.errorList[0]
            _this.setData({
                isPop: true
            })
            Dialog.alert({
                message: error.msg,
                width: '560rpx',
            }).then(() => {
                _this.setData({
                    isPop: false
                })
            });
            return false
        }
        App.globalData.userModel.apply({
            data: params
        }).then(json => {
            Dialog.alert({
                message: json.data,
                width: '560rpx',
            }).then(() => {
                wx.navigateBack();
            });
        })
    },

    /**
     * 移除图片
     */
    deleteImage: function (e) {
        let that = this;
        let fileList = that.data.fileList;
        let index = e.detail.index;
        fileList.splice(index, 1);
        that.setData({
            fileList: fileList
        })
    },

    /**
     * 上传图片
     */
    uploadImage: function (event) {
        const _this = this
        const {
            file
        } = event.detail;
        // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
        wx.uploadFile({
            url: App.globalData.serviceModel.$$prefix + '/user/uploadImg', // 仅为示例，非真实的接口地址
            filePath: file.path,
            name: 'userImg',
            accept: 'image',
            formData: {},
            header: {
                'token': wx.getStorageSync('token')
            },
            success(res) {
                var data = JSON.parse(res.data);
                if (data.code != 200) {
                    Toast("上传失败" + data.msg)
                    return;
                }
                data = data.data
                // 上传完成需要更新 fileList
              const { fileList = []} = _this.data;
                fileList.push({
                    ...file,
                    url: data.domain_url,
                    pic: data.pic
                });
                _this.setData({fileList});
            }
        });
    },

    /**
     * 拨打平台电话
     */
    tel: function () {
      wx.makePhoneCall({ phoneNumber: App.globalData.config.configList.phone})
    },


    onLoad: function (options) {
        const _this = this
        this.initValidate()
        this.getData()
    },

    getData(){
        const _this = this
        App.globalData.userModel.getStoreApply({
            data:{

            }
        }).then(json=>{
            _this.setData({
                apply:json.data,
                fileList:json.data.domain_attach
            })
        })
    },

    //从地图选点插件返回后，在页面的onShow生命周期函数中能够调用插件接口，取得选点结果对象
    onShow() {
        this.setData({ contract: App.globalData.config.configList.contract})
        let location = chooseLocation.getLocation(); // 如果点击确认选点按钮，则返回选点结果对象，否则返回null
        if (location) {
            this.setData({
                "apply.contact_addr_lng": location.longitude,
                "apply.contact_addr_lat": location.latitude,
                'apply.contact_addr': location.address
            })
        }
    },

    /**
     * 初始化验证信息
     */
    initValidate() {
        // 验证字段的规则
        var rules = {
            contact_name: {
                required: true,
            },
            contact_phone: {
                required: true,
            },
            company_name: {
                required: true,
            },
            contact_addr: {
                required: true,
            },
        }
        // 验证字段的提示信息，若不传则调用默认的信息
        var messages = {
            contact_name: {
                required: '请填写申请人姓名'
            },
            contact_phone: {
                required: '请填写手机号码'
            },
            company_name: {
                required: '请填写店铺名称',
            },
            contact_addr: {
                required: '请填写店铺地址',
            },
        }
        // 创建实例对象
        this.WxValidate = new WxValidate(rules, messages)
    },
})